<?php
/**
 * 我的考勤
 * @author coldarmy
 * **/
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Bll_Attendance_LeaveRecord');
apf_require_class('Bll_Attendance_ApplyType');
apf_require_class('Bll_Attendance_HistoryAnnual');
class Oa_Attendance_MyAnnualController extends Oa_Attendance_AbstractController {
    /** 上下分页 **/
    const PAGEROW = 12;

    public function handle_request() {
        $params = $this->request->get_parameters();

        $list = $this->get_apply_list($params);
        $this->request->set_attribute('list',$list);
        $this->request->set_attribute('count',count($list)-1);
        $t_bll = new Bll_Attendance_ApplyType();
        $type = $t_bll->get_all_leave_type();
        $this->request->set_attribute('type',$type);
        $this->request->set_attribute('annual',$this->get_all_annual());
        $this->request->set_attribute('params',$params);
        if(date('H') > 0 && date('H') < 12) {
            $ma = '上午';
        } else {
            $ma = '下午';
        }
        $this->page['curr_time'] = date('Y').'年'.date('m').'月'.date('d')."日&nbsp;&nbsp;&nbsp;星期".Tools_Tools::get_instance()->convert_chinese_week(date('w'))."&nbsp;&nbsp;&nbsp;&nbsp;".$ma."&nbsp;&nbsp;&nbsp;".date('h:i');
        $this->request->set_attribute('page',$this->page);

        return 'Oa_Attendance_MyAnnual';
    }




    /**
     * 根据时间点获取7天的数据
     * access_date >= $start AND access_date <= $end
     * @param mixed $params
     * @access private
     * @return void
     */
    private function get_apply_list($params) {
        $result = array();
        //$this->uinfo['user_id'] = 113;
        //如果用户未登录，返回空数组
        if(empty($this->uinfo['user_id'])) {
            return $result;
        }
        apf_require_class('Tools_Tools');
        $where['user_id'] = $this->uinfo['user_id'];
        $where['type'] = '年假';
        if($params['type']) {
            $where['type'] = $params['type'];
        }
        if($params['status']) {
            $where['status'] = $params['status'];
        }
        $page = 1;
        if($params['p'] && $params['p'] > 1) {
            $page = $params['p'];
        }
        $bll = new Bll_Attendance_LeaveRecord();
        $count = $bll->get_count_by_condition($where);
        if($count <= 0) {
            return $result;
        }
        $result = $bll->get_leave_record_data($where,self::PAGEROW,$page);
        $result = $this->rebuild_data($result);
        if($count > self::PAGEROW) {
            $url_params = $where;
            unset($url_params['user_id']);
            $multipage = $this->build_multiPage_info($count,$page,$url_params);
            $this->request->set_attribute('multipage',$multipage);
        }

        return $result;
    }

    /**
     * 重组列表数据
     *
     * @param mixed $data
     * @access private
     * @return void
     */
    private function rebuild_data($data){
        $result = array();
        if(empty($data)) {
            return $result;
        }
        $leaveids = array();
        foreach ($data as $key => $val) {
           $leaveids[] = $val['id'];
           $userids[] = $val['r_user_id'];
           if($val['b_user_id']) {
                $userids[] = $val['b_user_id'];
           }
        }
        apf_require_class('Bll_Attendance_LeaveRecordLog');
        $l_bll = new Bll_Attendance_LeaveRecordLog();
        $leave = $l_bll->get_log_by_ids($leaveids);
        //重新组装数据
        if($leave) {
            foreach ($leave as $k => $v) {
                $deallog[$v['leave_id']][$v['operator']] = $v;
            }
        }
        apf_require_class('Bll_Attendance_UserInfo');
        $u_bll = new Bll_Attendance_UserInfo();
        $user  = $u_bll->get_user_by_ids($userids);
        if($user) {
            foreach ($user as $k => $v) {
                $alluser[$v['user_id']] = $v;
            }
        }
        foreach ($data as $key => $val) {
            $val['can_cancel'] = 0;
            $val['need'] = 0;
           $val['contact_time'] = $this->format_date($val['start_time'])." - ".$this->format_date($val['end_time']);
           $val['apply_time'] = $this->format_date($val['apply_time']);
           //批准人
           $val['passinfo'] = "";
           if($val['r_user_id'] && $val['is_cancel'] != 1) {
               if($val['status']) {
                    $status = $val['status'];
               }
               $log = $deallog[$val['leave_id']][$val['r_user_id']];
               if($log && $log > 1){
                    $status = $log == 2?'已批准':'已驳回';
               }
               $val['passinfo'] .= $user[$val['r_user_id']]['name']."($status)";
           }
           if($val['b_user_id'] && $val['is_cancel'] != 1) {
               if($val['status']) {
                    $status = $val['status'];
               }
               $log = $deallog[$val['leave_id']][$val['b_user_id']];
               if($log && $log > 1){
                    $status = $log == 2?'已批准':'已驳回';
               }
               $val['passinfo'] .= ", ".$user[$val['b_user_id']]['name']."($status)";
           }
           $result[] = $val;
        }
        return $result;
    }

    /**
     * 格式化时间
     *
     * @param mixed $date
     * @access private
     * @return void
     */
    private function format_date($date) {
        $result = 0;
        if(empty($date)) {
            return $result;
        }
        $result = substr($date,0,4).'年'.intval(substr($date,5,2)).'月'.intval(substr($date,8,2)).'日';
        return $result;
    }

    /**
     * 获取翻页
     * @param unknown_type $totalnum 总的页数
     * @param unknown_type $page_num 当前第几页
     * @param array $url_params
     * @param int $uid 用户ID
     */
    public function build_multiPage_info($totalnum,$page_num = 0,$url_params = array()) {
        apf_require_class('Tools_MultiPage');
        //翻页信息
        $url = "/myannual/";
        $total_rows = $totalnum;
        if($page_num <= 0){$page_num = 1;}
        $url_params['p'] = $page_num;

        $multipage = Tools_MultiPage::multiPage($url,$url_params, '', $total_rows, self::PAGEROW, 10);
        return $multipage;
    }

}
